import sys, re, os

a = sys.argv[1]
p0 = a + r'\res'


def r0(p):
    f = open(p0 + p, 'r')
    s = f.read()
    f.close()
    return s


l = r0(r'\values\public.xml')
l1 = re.findall('(?<=<public type="drawable" name=")[\w\.]+?(?=" id="0x\w+?" />)', l)
l2 = re.findall('(?<=<public type="mipmap" name=")[\w\.]+?(?=" id="0x\w+?" />)', l)

d1 = {}
d2 = {}

for d3 in os.listdir(p0):
    if d3.startswith('drawable'):
        l = l1
        d = d1
    elif d3.startswith('mipmap'):
        l = l2
        d = d2
    else:
        continue
    p1 = p0 + '\\' + d3
    for f in os.listdir(p1):
        for z in ['.9.png', '.png', '.jpg', '.gif']:
            if f.endswith(z):
                i = f[:-len(z)]
                if i in l:
                    p = p1 + '\\' + f
                    s = os.path.getsize(p)
                    if i in d:
                        if s >= d[i][0]:
                            os.remove(p)
                            print d3 + '\\' + f
                            break
                        os.remove(p0 + '\\' + d[i][1])
                        print d[i][1]
                    d[i] = (s, d3 + '\\' + f)
                    break
        if len(os.listdir(p1)) == 0: os.rmdir(p1)

for d in os.listdir(p0):
    p1 = p0 + '\\' + d
    if not os.listdir(p1): os.rmdir(p1)
